define(["jquery", "easy-admin","step"], function ($, ea,step) {
    var form = layui.form
        layer = layui.layer
    step.render({
        elem: '#stepForm',
        filter: 'stepForm',
        width: '100%', //设置容器宽度
        stepWidth: '800px',
        height: '500px',
        stepItems: [{
            title: '基础信息'
        }, {
            title: '规格/价格'
        }, {
            title: '其它设置'
        }]
    });

    $('.pre').click(function () {
        let step_div = $('.step:visible')
        $('.step').hide()
        step_div.prev().show()
        step.pre('#stepForm');
    });

    $('.next').click(function () {
        let step_div = $('.step:visible')
        $('.step').hide()
        step_div.next().show()
        step.next('#stepForm');
    });

    $('.submit').click(function(){
        // var content=CKEDITOR.instances.describe.getData();//entity_content为textarea的ID
        // $('.describe').val(content)
        let is_submit = 1
        // 验证必填
        let ver  = $('input')
        ver.each(function(i){
            let that = ver[i]
            let verify = $(that).attr('lay-verify')
            if (verify == 'required') {
                if(!$(that).val()){
                    let placeholder = $(that).prop('placeholder')
                    layer.msg(placeholder)
                    is_submit = 0
                    return false
                }
            }
        })
        if(!is_submit){
            return false
        }
        ea.request.post({
            url:$(this).attr('data-url'),
            data:$('form').serializeArray()
        }, function (res) {
            ea.msg.success(res.msg, function () {
               
                window.parent.location.reload();
            });
        });
    })

    $('form').submit(function(){
        return false
    })

    form.on('radio(spec_type)',function(){
        $('.one').hide()
        $('.more').hide()
        $('.'+$(this).val()).show()
    })

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'shop.goods/index',
        add_url: 'shop.goods/add',
        edit_url: 'shop.goods/edit',
        delete_url: 'shop.goods/delete',
        export_url: 'shop.goods/export',
        modify_url: 'shop.goods/modify',
        stock_url: 'shop.goods/stock',
    };

    var Controller = {

        index: function () {
            ea.table.render({
                init: init,
                toolbar: ['refresh','add',
                    'delete'],
                // where:{cate_id:$("input[name='cate_id']").val(),store_id:$("input[name='store_id']").val()},
                cols: [[
                    {type: "checkbox"},
                    {field: 'products_id', width: 80, title: 'ID'},
                    {field: 'cate.cate_name', minWidth: 80, title: '商品分类', search: false},
                    {field: 'products_title', minWidth: 80, title: '商品名称'},
                    {field: 'cover_picture', minWidth: 80, title: '缩略图', search: false, templet: ea.table.image,search:false},
                    {field: 'price', width: 100, title: '原价', templet: ea.table.price,search:false},
                    {field: 'activity_price', width: 100, title: '活动价', templet: ea.table.member_price,search:false},
                    {field: 'total_stock', width: 100, title: '库存统计',search:false},
                    {field: 'stock', width: 100, title: '剩余库存',search:false},
                    {field: 'sort', width: 80, title: '排序', edit: 'text',search:false},
                    {field: 'is_shelves', title: '上架状态', width: 100, selectList: {0: '下架', 1: '上架'}, templet: ea.table.switch},
                    {field: 'create_time', width: 180, title: '创建时间', search: 'range',search:false},
                    {
                        width: 250,
                        title: '操作',
                        templet: ea.table.tool,
                        operat: [
                            [{
                                text: '编辑',
                                url: init.edit_url,
                                field:'products_id',
                                method: 'open',
                                auth: 'edit',
                                class: 'layui-btn layui-btn-xs layui-btn-success',
                                extend: 'data-full="true"',
                            }, {
                                title:'确定删除？',
                                text: '删除',
                                field:'products_id',
                                url: init.delete_url,
                                method: 'delete',
                                class: 'layui-btn layui-btn-danger layui-btn-xs',
                            },],
                            ]
                    }
                ]],
            });

            ea.listen();
        },
        add: function () {
            ea.listen();
        },
        edit: function () {
            ea.listen();
        },
        stock: function () {
            ea.listen();
        },
        copy: function () {
            ea.listen();
        },
    };
    return Controller;
});